package com.mazaiting.redeye.module.me

import android.annotation.SuppressLint
import android.graphics.Bitmap
import android.os.Bundle
import android.text.TextUtils
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.fragment.app.Fragment
import cn.jpush.im.android.api.callback.GetAvatarBitmapCallback
import cn.jpush.im.android.api.model.UserInfo
import com.chad.library.adapter.base.BaseQuickAdapter
import com.mazaiting.accessibility.base.Event
import com.mazaiting.easy.app.IApplicationComponent
import com.mazaiting.easy.base.fragment.BaseRefreshFragment
import com.mazaiting.log.L
import com.mazaiting.redeye.R
import com.mazaiting.redeye.base.ApplicationComponentImpl
import com.mazaiting.redeye.module.me.dd.DdActivity
import com.mazaiting.screen.picture.ScreenActivity
import com.mazaiting.screen.video.VideoActivity
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode


/**
 * 我的碎片
 */
class MeFragment : BaseRefreshFragment<MePresenter>(), MeContract.View {

    companion object {
        /**
         * 实例化当前 Fragment
         */
        fun newInstance(): Fragment = MeFragment()

        /**
         * 列表数据
         */
        private val mList = arrayListOf(
            "钉钉自动打卡",
            "截屏",
            "录屏",
            "天天酷跑"
        )
    }

    /**
     * 头像
     */
    private var mAvatarView: ImageView? = null
    /**
     * 昵称
     */
    private var mNickName: TextView? = null
    /**
     * 性别
     */
    private var mGenderView: ImageView? = null
    /**
     * 签名
     */
    private var mSignature: TextView? = null
    /**
     * 用户信息
     */
    private var mHeaderInfo: UserInfo? = null


    override fun lazyFetchData() {
        presenter?.loadInformation()
    }

    override fun loadInformationSuccess() {
        L.d("加载成功")
    }

    override fun loadInformationError(msg: String) {
        L.d(msg)
    }


    override fun bindView(view: View?, savedInstanceState: Bundle?) {
        EventBus.getDefault().register(this)
        super.bindView(view, savedInstanceState)
        initHeaderView()
        initBodyView()

        // 下拉刷新
        swipeLayout.setOnRefreshListener {
            swipeLayout.isRefreshing = true
            presenter?.loadInformation()
            initHeaderData()
            swipeLayout.isRefreshing = false
        }
    }

    /**
     * 初始化列表
     */
    private fun initBodyView() {
        // 设置数据
        adapter.setNewData(mList)
        // 设置点击事件
        adapter.setOnItemClickListener { _, _, position ->
            when (position) {
                // 钉钉自动打卡
                0 -> context?.applicationContext?.let { context -> DdActivity.newInstance(context) }
                // 截屏
                1 -> context?.applicationContext?.let { context -> ScreenActivity.newInstance(context) }
                // 录屏
                2 -> context?.applicationContext?.let { context -> VideoActivity.newInstance(context) }
                // 天天酷跑
                3 -> { }
            }
//            Toast.makeText(context, "位置: $position", Toast.LENGTH_SHORT).show()
        }
    }

    /**
     * 初始头部
     */
    @SuppressLint("InflateParams")
    private fun initHeaderView() {
        // 加载布局
        val headerView = layoutInflater.inflate(R.layout.me_header, null)
        // 获取头像
        mAvatarView = headerView.findViewById(R.id.me_avatar)
        // 获取昵称
        mNickName = headerView.findViewById(R.id.me_nickname)
        // 性别
        mGenderView = headerView.findViewById(R.id.me_gender)
        // 签名
        mSignature = headerView.findViewById(R.id.me_signature)
        // 头像点击
        mAvatarView?.setOnClickListener {
            this.context?.let { AvatarActivity.newInstance(it, "") }
        }
        // 头部设置点击事件
        headerView.setOnClickListener {
            mHeaderInfo?.let { it1 -> context?.let { it2 -> UserInfoActivity.newInstance(it2, it1) } }
        }

        adapter.setHeaderView(headerView)
        initHeaderData()
    }

    /**
     * 初始化头部数据
     */
    private fun initHeaderData() {
        // 获取用户信息
        val headerInfo = presenter?.loadHeaderInfo()
        if (null != headerInfo) {
            // 设置头像
            headerInfo.getAvatarBitmap(object : GetAvatarBitmapCallback() {
                override fun gotResult(code: Int, msg: String?, bitmap: Bitmap?) {
                    L.d("code: $code, msg: $msg")
                    mAvatarView?.setImageBitmap(bitmap)
                }
            })
            // 设置昵称
            mNickName?.text =
                if (!TextUtils.isEmpty(headerInfo.nickname)) {
                    headerInfo.nickname
                } else {
                    headerInfo.userName
                }
            // 设置性别
            when {
                headerInfo.gender == UserInfo.Gender.female -> mGenderView?.setImageResource(R.mipmap.ic_female)
                headerInfo.gender == UserInfo.Gender.male -> mGenderView?.setImageResource(R.mipmap.ic_male)
                headerInfo.gender == UserInfo.Gender.unknown -> mGenderView?.setImageResource(R.mipmap.ic_male)
            }
            // 设置签名
            mSignature?.text = headerInfo.signature
            // 设置用户名
//            L.d(headerInfo.toJson())
            // 赋值
            mHeaderInfo = headerInfo
        }
    }

    override fun getContentLayout(): Int = R.layout.fragment_me

    override fun inject(applicationComponent: IApplicationComponent?) {
        DaggerMeComponent
            .builder()
            .applicationComponentImpl(applicationComponent as ApplicationComponentImpl)
            .build()
            .inject(this)
    }

    override fun setAdapter(): BaseQuickAdapter<*, *> = MeAdapter()

    override fun onDestroy() {
        EventBus.getDefault().unregister(this)
        super.onDestroy()
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onEvent(event: Event) {
        L.d("test")
    }
}
